<?php
class general_student{
     function __construct($student_id="0"){
         $this->student_id=$student_id;
         $this->yr_lvl = 1;
     }
     function assess_added_subjects($data){
         #this will prepare the post subjects to be inclded in form
         $subjects = array();
         foreach($data['subjects'] as $datum){
             if($data['subj_code_'.$datum]=="add"){
                 array_push($subjects,array('subj_code'=>$datum,'description'=>$data['description_'.$datum],'units'=>$data['units_'.$datum]));
             }
         }
         return $subjects;
     }
     
     function assess_drop_subjects($yr_lvl,$semester){
         #this will show what to drop
         $query = sql_functions::select_query("* from enroll_".$_SESSION['syear']." where student_id='".$this->student_id."'"." and yr_lvl='".$yr_lvl."' and semester='".$semester."'");
         $row= sql_functions::fetch_to_array($query);
         #prepare the subjects
         $subjects = explode("|",$row[0]['subject']);
         $idadrop = array();
         foreach($subjects as $subject){
             if($subject==""){
             #do not include in array                 
             }else{
                 array_push($idadrop,"'".$subject."'");   
             } 
         }
         $idadrop = implode(",",$idadrop);
         $query = sql_functions::select_query("* from stud_".$this->student_id." where subj_code in (".$idadrop.")");
         $row =  sql_functions::fetch_to_array($query);
         return $row;
         //return $row;
     }
     
     
     function get_assessed($semester,$profile,$yr_lvl=NULL){//NOte: this function does not allow additon of extra subjects
         #checks whether a student table exists 
              $table_exists = sql_functions::select_query("count(table_name) as bilang from tables where table_schema='ssc_reg_sis' and table_name='stud_".$this->student_id."'","information_schema");                 
              $table_count = sql_functions::fetch_to_array($table_exists);
             
          #get the name of the table course
              $course_query =sql_functions::select_query("code from codes where category='prospectus' and description='".$profile[0]['course']."-".$profile[0]['major']."'");
              $his_course =sql_functions::fetch_to_array($course_query);
          #...and create if not existing based from course
              if($table_count[0]['bilang']==0){
                sql_functions::apply_query("create table if not exists stud_".$this->student_id." as (select * from ".$his_course[0]['code'].")");             
              }   
                  
          #gets the current year level
             // $current_level = specific_student::get_year_specific_level($profile[0]['course'],$profile[0]['major'],$semester,$this->student_id);
             #for the sake of temporary grade entry line 24 is forced
             $current_level = $yr_lvl;
          
          #this will assess the previos year level if exists
            $yr_lvl = $current_level;
            
          #this prohibits zero result, zero result pertains to first year
            $yr_lvl_minus_1 = $yr_lvl-1;
            
             #@algorithm starts here
                #creates array for the enrolling the selected subjects
                 $my_subjects=array();
                 
             #1. Check the subjects from the previous years 
 /*              $query_two =sql_functions::select_query("* FROM stud_".$this->student_id." where yr_lvl=".$yr_lvl_minus_1." and semester=".$semester." and (status is null or status='' or status='failed') group by subj_code");
              $prev_subjects = sql_functions::fetch_to_array($query_two);
              #enter to array
              foreach($prev_subjects as $to_be_enrolled){
                  array_push($my_subjects,array('subj_code'=>$to_be_enrolled['subj_code'],'description'=>$to_be_enrolled['description'],'units'=>$to_be_enrolled['units']));
              }*/
              
             #2. Get the subjects of the present year w/o pre-requisite 
              $query_one =sql_functions::select_query("* FROM stud_".$this->student_id." where yr_lvl=".$yr_lvl." and semester=".$semester." and (subj_preq is null or subj_preq='') and (status is null or status='' or status='failed' or status='dropped' or status='inc') group by subj_code");
              $present_subj_w_o_preq=sql_functions::fetch_to_array($query_one);
              #enter to array
              foreach($present_subj_w_o_preq as $to_be_enrolled){
                  array_push($my_subjects,array('subj_code'=>$to_be_enrolled['subj_code'],'description'=>$to_be_enrolled['description'],'units'=>$to_be_enrolled['units']));
              }
              
              #3. Get the subjects of the present year w/ pre-req and check
             $query_three=sql_functions::select_query("una.subj_code as subj_code,una.units as units,una.description as description,duwa.status as remarks FROM stud_".$this->student_id." as una left join stud_".$this->student_id." as duwa on una.subj_preq=duwa.subj_code where una.yr_lvl=".$yr_lvl." and una.semester=".$semester." and (una.subj_preq is not null or una.subj_preq<>'')"); 
              $subseq_subjects=sql_functions::fetch_to_array($query_three);
            
             #3.1algorithm to check duplicates
             $palpak=array();
                $kay_nano=array();
                $siya = $subseq_subjects;
                foreach($siya as $ikaw=>$key){
                        if($key['remarks']=='' || $key['remarks']=='failed' || $key['remarks']=='dropped' || $key['remarks']=='inc'){    
                        array_push($palpak,$key['subj_code']);    //get the key if failed remarks exists
                        array_push($kay_nano,$key['subj_preq']);
                        unset($siya[$ikaw]);            //remove if from the array
                    }
                }
                foreach($siya as $ikaw=>$key){
                    foreach($palpak as $hahanapon){
                           if($key['subj_code']==$hahanapon){
                               unset($siya[$ikaw]);
                                break;
                            }
                         }
                   
                }
                $duplicates = array();
                foreach($siya as $ikaw=>$key){
                   
                    if(in_array($key['subj_code'],$duplicates)){
                        unset($siya[$ikaw]);
                    }else{
                         array_push($duplicates,$key['subj_code']);
                    }
                }
                $subseq_subjects = $siya;
             #enter to array
              foreach($subseq_subjects as $to_be_enrolled){
                  array_push($my_subjects,array('subj_code'=>$to_be_enrolled['subj_code'],'description'=>$to_be_enrolled['description'],'units'=>$to_be_enrolled['units']));
              }
              
              foreach($my_subjects as $subjects){ 
                $sum=$sum + $subjects['units'];               
                  #this will check if the number of units exceed the allowed, the value should be dynamic
                      if($sum>37){ 
                          $sum=$sum- $subjects['units']; //used to maintain accuracy of sum units              
                             break;
                      }
              }
       #end of the algorithm for assessment of subjects
              
           #this is for the enrollment storage of subjects and needed data
             sql_functions::apply_query("create table if not exists  enroll_".$_SESSION['syear']./*date('Y') */" like enroll_year");
             
          #checks whether there is already a record in the enroll tabel  for the subjects
            $query_four=sql_functions::select_query("student_id  from enroll_".$_SESSION['syear']." where student_id='".$this->student_id."' and yr_lvl=".$yr_lvl." and semester=".$semester." and (subject is not null or subject<>'')");
           #...using conditional statement
              if(sql_functions::count_rows($query_four)==0){  
                           
                   #checks whether the student is regular or irregular  //todo:determine if irrefular or regular   
                  #1.                
                    $w_o_and_w_preq_count = count($present_subj_w_o_preq) + count($subseq_subjects);
                    $count_of_sub = sql_functions::select_query("subj_code FROM stud_".$this->student_id." where yr_lvl=".$yr_lvl." and semester=".$semester." group by subj_code");
                    if(sql_functions::count_rows($count_of_sub)==$w_o_and_w_preq_count){
                        $stud_type = "regular";
                    }else{
                        $stud_type="irregular";
                    }
                   #2. Check if a record or exist. Update or insert the remarks of being a regular or irregular
                   $query_five = sql_functions::select_query("* from enroll_".$_SESSION['syear']." where student_id='".$this->student_id."' and yr_lvl='".$yr_lvl."' and semester='".$semester."'");
                   if(sql_functions::count_rows($query_five)==0){
                       sql_functions::apply_query("insert into enroll_".$_SESSION['syear']."(student_id,degree_name,course,major,yr_lvl,semester,stud_type)values('".$this->student_id."','".$profile[0]['degree_name']."','".$profile[0]['course']."','".$profile[0]['major']."','".$yr_lvl."','".$semester."','".$stud_type."')");                          
                   }else{
                       sql_functions::apply_query("update enroll_".$_SESSION['syear']." set degree_name='".$profile[0]['degree_name']."', course='".$profile[0]['course']."',major='".$profile[0]['major']."',stud_type='".$stud_type."' where student_id='".$this->student_id."' and yr_lvl='".$yr_lvl."' and semester='".$semester."'");
                   }

 
                       #return assessed subjects        
                        return $my_subjects;
              }else{
                  
                        #return none either addition or dropping of subjects
                        return "none";
              }      
          
     }
     
     function get_year_lvl($student_id,$semester){
          #this functions gets the semester and shows the current level of student returns year level
              $query_one = sql_functions::select_query("yr_lvl FROM stud_".$student_id." where subj_type='major' and semester=".$semester." and (status is null OR STATUS='' or status='failed') order by yr_lvl,semester,subj_code limit 1");
              $current_level = sql_functions::fetch_to_array($query_one);
              return $current_level;
     }
     
     function check_valid_user(){
         #this function checks if the user has a profile returns boolean
         $query=sql_functions::select_query("stud_id from reg_profile where stud_id='".$this->student_id."'");
         $count=sql_functions::count_rows($query);
         if($count>1 || $count==0){
             return false;
         }else{
             return true;
         }
     }
     
     function get_cog($yr_lvl,$semester){
         #this function gets records from dbase based only from year level,semester and returns as array
         #@("1","2");
         $query=sql_functions::select_query("* from stud_".$this->student_id." where yr_lvl=".$yr_lvl." and semester=".$semester." group by subj_code order by id asc");
         $grades =sql_functions::fetch_to_array($query);
         return $grades;
     }
     
     function get_evaluation($yr_lvl,$semester){
         #this function gets the records from dbase for all year level present
         $query="subj_code,subj_preq,description,units,substring(taken_1,1,6) as taken,yr_lvl,semester,grade_1,status FROM stud_".$this->student_id." where yr_lvl<='".$yr_lvl."'";
         if($semester==1){
             $query.=" and (yr_lvl='".$yr_lvl."' and semester<>'2')";
         } 
         $query.= " group by subj_code order by yr_lvl,semester,id asc";
        $query =  sql_functions::select_query($query);
        $record=sql_functions::fetch_to_array($query);
         return($record);
         
     }
     
     function show_what_to_add(){
         #this function will show all the subjects from first year to fourthyear
         $query = sql_functions::select_query("* from stud_".$this->student_id." group by subj_code order by yr_lvl,semester,id asc");
         $row = sql_functions::fetch_to_array($query);
         return $row;
     }
     
     function show_what_to_drop($yr_lvl,$semester){
         #this function will show what subjects he can drop
         $query = sql_functions::select_query("* from enroll_".$_SESSION['syear']." where student_id='".$this->student_id."' and yr_lvl='".$yr_lvl."' and semester='".$semester."'");
         $row = sql_functions::fetch_to_array($query);
         return $row;
     }
     
     function get_profile(){
         #this function get all the profile of user in the dbase
         $query=sql_functions::select_query("* from reg_profile where stud_id='".$this->student_id."'");
         if(sql_functions::count_rows($query)==0){
             die("<p class='warnings'>You have problems with your records in the registrar.</p>");
         }
         $profile=sql_functions::fetch_to_array($query);
         return $profile;
     }
     
     function get_babayran($semester){
         #this function gets all the fees
         $query=sql_functions::select_query("*,count(assess_id) as count from config_assessment where year='".$_SESSION['syear']."'"./*date('Y') */" and semester='".$semester."' group by assess_id having count=1 order by assess_id asc");
         $row=sql_functions::fetch_to_array($query);
         return $row;
     }
     
     function my_assessment($data){
              #this function will assess the student
              $query = sql_functions::select_query("* from stud_".$_SESSION['profile_id']." where  yr_lvl=".$data['yr_lvl']." and semester=".$data['semester']." and (status = '' || status='assessed')","cashier");
              $row=sql_functions::fetch_to_array($query);
              return $row;  
            }
            

   
}
class specific_student extends general_student{
         function get_year_specific_level($course,$major,$semester,$student_id){
                    switch($course){
                        case 'BT':
                            #this functions gets the semester and shows the current level of student returns year level
                              $query_one = sql_functions::select_query("yr_lvl FROM stud_".$student_id." where subj_type='major' and semester=".$semester." and (status is null OR STATUS='' or status='failed') order by yr_lvl,semester,subj_code limit 1");
                              $current_level = sql_functions::fetch_to_array($query_one);
                              return $current_level[0]['yr_lvl'];
                            break;
                            
                        case 'BS':
                          $query_zero = sql_functions::select_query("units from stud_".$student_id." group by subj_code");
                              $row_zero = sql_functions::fetch_to_array($query_zero);
                              $sum=0;
                              foreach($row_zero as $isasum){
                                 $sum= $sum + $isasum['units']; 
                              }
                               #2. 
                               $needed_units_first = round(($sum * .25));
                               $needed_units_second = round(($sum * .25));; 
                             #@end                             
                               #check first(1st) year status                             
                             $query_two =  sql_functions::select_query("units from stud_".$student_id." where yr_lvl=1 and status='passed' group by subj_code");
                               $row_two = sql_functions::fetch_to_array($query_two);
                               #get the total units passed during first year
                                 $sum=0;
                                  foreach($row_two as $isasum){
                                     $sum= $sum + $isasum['units']; 
                                  }
                                  $earned_units = $sum;
                            
                        
                        
                        
                            break;

                        case 'BEED':    
                        case 'BSED':
                            #this functions gets the semester and shows the current level of student returns year level
                            
                               #get first the total units and 25% of it from the curriculum
                               #1.
                              $query_zero = sql_functions::select_query("units from stud_".$student_id." group by subj_code");
                              $row_zero = sql_functions::fetch_to_array($query_zero);
                              $sum=0;
                              foreach($row_zero as $isasum){
                                 $sum= $sum + $isasum['units']; 
                              }
                               #2. 
                               $needed_units = round(($sum * .25));
                             #@end                             
                               #check first(1st) year status                             
                             $query_two =  sql_functions::select_query("units from stud_".$student_id." where yr_lvl=1 and status='passed' group by subj_code");
                               $row_two = sql_functions::fetch_to_array($query_two);
                               #get the total units passed during first year
                                 $sum=0;
                                  foreach($row_two as $isasum){
                                     $sum= $sum + $isasum['units']; 
                                  }
                                  $earned_units = $sum;
                                #test if first year or proceed to algorithm 
                               if($needed_units>=$earned_units){
                                  $current_level = 1;
                                  return $current_level; 
                               }else{
                                  $query_one = sql_functions::select_query("yr_lvl FROM stud_".$student_id." where subj_type='major' and semester=".$semester." and (status is null OR STATUS='' or status='failed') order by yr_lvl,semester,subj_code limit 1");
                                  $current_level = sql_functions::fetch_to_array($query_one);
                                  return $current_level[0]['yr_lvl']; 
                               } 
                            break;           
                    }
         }
}
class irregular_student extends general_student{
    
}

class freshman extends general_student{
    
}
?>
